package com.mt.api.modules.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.mt.common.response.WebPageResponse;
import com.mt.common.response.WebResponseUtils;
import com.mt.api.modules.domain.AppCargo;
import com.mt.api.modules.domain.bo.StockCargoPageBo;
import com.mt.api.modules.domain.dto.StockStatQtyByCargoDto;
import com.mt.api.modules.manager.ICargoManager;
import com.mt.api.util.*;
import com.mt.api.modules.domain.AppStock;
import com.mt.api.modules.domain.vo.StockCargoRecordVo;
import com.mt.api.modules.manager.IStockManager;
import com.mt.api.modules.mapper.AppStockMapper;
import com.mt.api.modules.service.AppStockService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author plover
 * @description 服务实现
 * @date 2020-07-06
 **/
@Service
@RequiredArgsConstructor
public class AppStockServiceImpl extends ServiceImpl<AppStockMapper, AppStock> implements AppStockService {

    private final IStockManager stockManager;
    private final ICargoManager cargoManager;

    @Override
    public WebPageResponse<StockCargoRecordVo> recordWithCargo(StockCargoPageBo bo) {
        Page page = stockManager.getPageGroupByCargo(bo);
        List<StockStatQtyByCargoDto> records = page.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return WebResponseUtils.successPageV2(page);
        }
        //查询商品的库存明细信息
        List<Integer> cargoIds = records.stream().map(StockStatQtyByCargoDto::getCargoId).collect(Collectors.toList());
        List<AppStock> stockListByCargoId = stockManager.getStockByCargoIds(cargoIds);
        //查询商品基本信息
        List<AppCargo> cargoList = cargoManager.listByIds(cargoIds);

        List<StockCargoRecordVo> result = Lists.newArrayList();
        for (StockStatQtyByCargoDto record : records) {
            StockCargoRecordVo stockCargoRecordVo = new StockCargoRecordVo();
            stockCargoRecordVo.setCargoId(record.getCargoId());
            List<AppStock> stockList = stockListByCargoId.stream().filter(appStock -> Objects.equals(appStock.getCargoId(), record.getCargoId())).collect(Collectors.toList());
            List<StockCargoRecordVo.StockRecord> stockRecords = stockList.stream().map(appStock -> {
                StockCargoRecordVo.StockRecord stockRecord = new StockCargoRecordVo.StockRecord();
                stockRecord.setColor(appStock.getColor());
                stockRecord.setSize(appStock.getSize());
                stockRecord.setQty(appStock.getQty());
                return stockRecord;
            }).collect(Collectors.toList());
            stockRecords = ColorSizeSortUtil.stockSort(stockRecords);
            stockCargoRecordVo.setStockRecordList(stockRecords);
            int totalQty = stockRecords.stream().mapToInt(StockCargoRecordVo.StockRecord::getQty).sum();
            Optional<AppCargo> cargoOptional = cargoList.stream().filter(appCargo -> Objects.equals(appCargo.getId(), record.getCargoId())).findFirst();
            if (cargoOptional.isPresent()) {
                AppCargo cargo = cargoOptional.get();
                stockCargoRecordVo.setCargoNo(cargo.getCargoNo());
                stockCargoRecordVo.setCargoName(cargo.getCargoName());
                stockCargoRecordVo.setWeight(cargo.getWeight());
                stockCargoRecordVo.setPicUrl(cargo.getPicUrl());
                stockCargoRecordVo.setRemark(cargo.getRemark());
                stockCargoRecordVo.setQty(totalQty);
                stockCargoRecordVo.setPurchasePrice(AmountUtil.divideHundred(cargo.getPurchasePrice()));
                stockCargoRecordVo.setWholesalePrice(AmountUtil.divideHundred(cargo.getWholesalePrice()));
                result.add(stockCargoRecordVo);
            }
        }
        page.setRecords(result);
        return WebResponseUtils.successPageV2(page);
    }
}